package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;

/**
 * 患者查询病历详情
 */
public class CaseHistoryInfoActivity extends AppCompatActivity {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView userNumber,userName,userAge,userSex,departmentsName;

    private TextView userMainSuit,userPhi,previousHistory,drugAllergyHistory,personalAndFamilyHistory,healthCheckup,auxiliaryExamination,tentativeDiagnosis;

    private String loginDoctorId,registrationLogId,userId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case_history_info);
//        实例化
        dbOpenHelper = new DBOpenHelper(CaseHistoryInfoActivity.this, "db_kindheartedness", null, 1);
//        获取当前登录医生Id
        Intent intent2 = getIntent();
        final Bundle bundle = intent2.getExtras();
        //        当前医生Id
        loginDoctorId = bundle.getString("loginDoctorId");
//        患者挂号Id
        registrationLogId = bundle.getString("registrationLogId");
//      获取各文本框和输入框控制权
        userNumber = findViewById(R.id.userNumber);
        userName = findViewById(R.id.userName);
        userAge = findViewById(R.id.userAge);
        userSex = findViewById(R.id.userSex);
        departmentsName = findViewById(R.id.departmentsName);
        userMainSuit = findViewById(R.id.userMainSuit);
        userPhi = findViewById(R.id.userPhi);
        previousHistory = findViewById(R.id.previousHistory);
        drugAllergyHistory = findViewById(R.id.drugAllergyHistory);
        personalAndFamilyHistory = findViewById(R.id.personalAndFamilyHistory);
        healthCheckup = findViewById(R.id.healthCheckup);
        auxiliaryExamination = findViewById(R.id.auxiliaryExamination);
        tentativeDiagnosis = findViewById(R.id.tentativeDiagnosis);
//      查询数据并回显
        findByRegistrationLogId();

    }

    /**
     * 通过患者挂号记录Id查询
     */
    private void findByRegistrationLogId() {
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        //        通过患者挂号记录ID查询患者用户ID
        Cursor queryCaseHistory = readableDatabase.query("case_history", null, "registration_log_id = ?", new String[]{registrationLogId}, null, null, null);
        String userNumberToStr = null,userNameToStr = null,userAgeToStr = null,userSexToStr = null,departmentsNameToStr = null;
        String userMainSuitToStr = null,userPhiToStr = null,previousHistoryToStr = null,drugAllergyHistoryToStr = null,personalAndFamilyHistoryToStr = null;
        String healthCheckupToStr = null,auxiliaryExaminationToStr = null,tentativeDiagnosisToStr = null;
        while (queryCaseHistory.moveToNext()) {
            userNameToStr = queryCaseHistory.getString(1);
            userNumberToStr = queryCaseHistory.getString(2);
            userSexToStr = queryCaseHistory.getString(3);
            userAgeToStr = queryCaseHistory.getString(4);
            departmentsNameToStr = queryCaseHistory.getString(5);
            userMainSuitToStr = queryCaseHistory.getString(6);
            userPhiToStr = queryCaseHistory.getString(7);
            previousHistoryToStr = queryCaseHistory.getString(8);
            drugAllergyHistoryToStr = queryCaseHistory.getString(9);
            personalAndFamilyHistoryToStr = queryCaseHistory.getString(10);
            healthCheckupToStr = queryCaseHistory.getString(11);
            auxiliaryExaminationToStr = queryCaseHistory.getString(12);
            tentativeDiagnosisToStr = queryCaseHistory.getString(13);
        }
        userNumber.setText(userNumberToStr);userName.setText(userNameToStr);
        userAge.setText(userAgeToStr);userSex.setText(userSexToStr);
        departmentsName.setText(departmentsNameToStr);
        userMainSuit.setText(userMainSuitToStr);userPhi.setText(userPhiToStr);
        previousHistory.setText(previousHistoryToStr);
        drugAllergyHistory.setText(drugAllergyHistoryToStr);
        personalAndFamilyHistory.setText(personalAndFamilyHistoryToStr);
        healthCheckup.setText(healthCheckupToStr);
        auxiliaryExamination.setText(auxiliaryExaminationToStr);
        tentativeDiagnosis.setText(tentativeDiagnosisToStr);
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}